package DataBaseQuery;

import DataType.MyDataType;

public class CompareCondition extends Condition {
	public enum MyCompare {
		EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL,NOT_EQUAL
	};

	MyCompare compare;
	MyExpression leftExpression;
	MyExpression rightExpression;

	public CompareCondition(MyExpression val1, MyCompare comp, MyExpression val2) {
		compare = comp;
		leftExpression = val1;
		rightExpression = val2;
	}

	@Override
	public boolean isTrue() {
		MyDataType val1 = leftExpression.getValue();
		MyDataType val2 = rightExpression.getValue();

		if (val1 != null && val2 != null) {
			if (compare == MyCompare.EQUAL && val1.isEqual(val2)) {
				return true;
			}
			if (compare == MyCompare.NOT_EQUAL && !val1.isEqual(val2)) {
				return true;
			}
			if (compare == MyCompare.LESS_THAN && val1.isLessThan(val2)) {
				return true;
			}
			if (compare == MyCompare.LESS_THAN_OR_EQUAL 
					&& (val1.isEqual(val2) || val1.isLessThan(val2))) 
			{
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean isTrue(RegularView view) {
		MyDataType val1 = leftExpression.getValue(view);
		MyDataType val2 = rightExpression.getValue(view);

		if (val1 != null && val2 != null) {
			if (compare == MyCompare.EQUAL && val1.isEqual(val2)) {
				return true;
			}
			if (compare == MyCompare.NOT_EQUAL && !val1.isEqual(val2)) {
				return true;
			}
			if (compare == MyCompare.LESS_THAN && val1.isLessThan(val2)) {
				return true;
			}
			if (compare == MyCompare.LESS_THAN_OR_EQUAL 
					&& (val1.isEqual(val2) || val1.isLessThan(val2))) 
			{
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean checkInvalidVarible()
	{
		return (leftExpression.isValid() && rightExpression.isValid());
	}

}
